﻿using ERPSystem.Common.Domains.CustomerProfileCommon;
using ERPSystem.Common.DTO;
using ERPSystem.Common.Infrastructure.IRepository;
using ERPSystem.Enum.CommonErrorCodes;
using MediatR;
using SqlSugar;

namespace BasicInformationCommon.API.BasicInformationCommons.CustomerProfileesReads
{
    public class CustomerProfileQueryHandler : IRequestHandler<CustomerProfileQueryCommand, Result<PageResultDto<CustomerProfileQueryDto>>>
    {
        private readonly ISqlSugarClient _db;

        public CustomerProfileQueryHandler(ISqlSugarClient db)
        {
            _db = db;
        }
        /// <summary>
        /// 查询客户档案信息业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result<PageResultDto<CustomerProfileQueryDto>>> Handle(CustomerProfileQueryCommand request, CancellationToken cancellationToken)
        {
            var result = new Result<PageResultDto<CustomerProfileQueryDto>>();
            var page = new PageResultDto<CustomerProfileQueryDto>() { TotalCount=0,TotalPage=0,List=new List<CustomerProfileQueryDto>()};
            try
            {
                RefAsync<int> totalCount = 0;
                RefAsync<int> totalPage = 0;
                var list = _db.Queryable<CustomerProfile, ClientType, CreditRating>((a,b,c)=>a.ClientTypeId==b.ClientTypeId&&a.CreditRatingId==c.CreditRatingId).Select((a,b,c)=> new CustomerProfileQueryDto()
                {
                    CustomerProfileId = a.CustomerProfileId,
                    CustomerName = a.CustomerName,
                    WarehouseName = a.WarehouseName,
                    Abbreviation = a.Abbreviation,
                    Address = a.Address,
                    Zip = a.Zip,
                    Phone = a.Phone,
                    CreditRatingId = c.CreditRatingId,
                    CreditLimit = a.CreditLimit,
                    ClientTypeId = b.ClientTypeId,
                    Remark = a.Remark,
                    States = a.States,
                    Bank = a.Bank,
                    AddressTelePhone = a.AddressTelePhone,
                    AccountNumber = a.AccountNumber,
                    TaxIdNumber = a.TaxIdNumber,
                    BusinessContacts = a.BusinessContacts,
                    BusinessTel = a.BusinessTel,
                    FinanceContacts = a.FinanceContacts,
                    FinanceTel = a.FinanceTel,
                    ClientTypeName = b.ClientTypeName,
                    CreditRatingName = c.CreditRatingName,
                });
                //查询条件
                if (!string.IsNullOrEmpty(request.CustomerName))
                {
                    list = list.Where(c => c.CustomerName.Contains(request.CustomerName));
                }
                if (!string.IsNullOrEmpty(request.Phones))
                {
                    list = list.Where(c => c.BusinessContacts==request.Phones);
                }
                if (!string.IsNullOrEmpty(request.Phones))
                {
                    list = list.Where(c => c.FinanceContacts == request.Phones);
                }
                var res =await list.ToPageListAsync(request.PageIndex, request.PageSize, totalCount, totalPage);
                page.TotalCount = totalCount;
                page.TotalPage = totalPage;
                page.List = res;
                if (page.List.Any())
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = page;
                    result.Msg = "获取客户档案信息成功";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
